/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductView.java
 *
 * Created on 2011-feb-22, 15:54:31
 */
package edu.chalmers.se.tda367.gui.panel;

import edu.chalmers.se.tda367.main.Pluggis;
import edu.chalmers.se.tda367.model.User;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ImageIcon;
import edu.chalmers.se.tda367.observer.GlobalObserverList;
import java.awt.Color;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.JLabel;

/**
 *
 * @author Pierre Reimertz
 */
public class UserPanel extends javax.swing.JPanel implements PropertyChangeListener {

    GlobalObserverList observers = GlobalObserverList.getInstance();
    private User userReference;
    private String pictureUrl;

    /** Creates new form userPanel */
    public UserPanel() {
        initComponents();
        observers.addObserver(this);

    }

    public Icon getPicture() {
        return pictureLabel.getIcon();
    }

    public UserPanel(String name, String imageUrl, User user) {
        initComponents();
        observers.addObserver(this);
        userReference = user;
        nameLabel.setText(name);

        ImageIcon icon = new ImageIcon(imageUrl);
        Image image = icon.getImage().getScaledInstance(120, 150, Image.SCALE_SMOOTH);
        icon.setImage(image);
        this.pictureLabel.setIcon(icon);

        pictureUrl = imageUrl;
        if (userReference.getStatus()) {
            this.setBackground(new Color(255, 255, 102));
            System.out.println(userReference.getStatus());
        }
        this.validate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        pictureLabel = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getResourceMap(UserPanel.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(130, 150));
        setLayout(new java.awt.BorderLayout());

        nameLabel.setFont(resourceMap.getFont("nameLabel.font")); // NOI18N
        nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        nameLabel.setName("nameLabel"); // NOI18N
        nameLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nameLabelMouseClicked(evt);
            }
        });
        add(nameLabel, java.awt.BorderLayout.PAGE_END);

        pictureLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pictureLabel.setText(resourceMap.getString("pictureLabel.text")); // NOI18N
        pictureLabel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pictureLabel.setName("pictureLabel"); // NOI18N
        pictureLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pictureLabelMouseClicked(evt);
            }
        });
        add(pictureLabel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void nameLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nameLabelMouseClicked

        //observers.firePropertyChange("UserPanel.clickedUser", null, this);
        Pluggis g = Pluggis.getInstance();

        g.setSelectedUser(userReference);
        System.out.println("klickad!");
    }//GEN-LAST:event_nameLabelMouseClicked

    private void pictureLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pictureLabelMouseClicked

        Pluggis g = Pluggis.getInstance();

        g.setSelectedUser(userReference);
        System.out.println("klickad!");
    }//GEN-LAST:event_pictureLabelMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel pictureLabel;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent pce) {
        String change = pce.getPropertyName();
        if (change.equals("GUI.RepaintView")) {
            this.repaint();
        }
    }
}
